<?php
class Model_Esl extends Model_WotHub {
    
    public function setEslTeam($eslId= null) {
        if (isset($_POST['frm_eslid']) === true) {
            $sql = 'DELETE FROM esl_team WHERE esl_id = :eslid';
            $this->executerSql($sql, array(':eslid' => $_POST['frm_eslid']));
            $sql = 'DELETE FROM esl_membres WHERE esl_id = :eslid';
            $this->executerSql($sql, array(':eslid' => $_POST['frm_eslid']));
            if ($_POST['frm_action'] === 'Supprimer') {
                SmartbokMessage::addMessage(SmartbokMessage::INFO, 'Equipe '.$_POST['frm_name'].' supprimée');
                return true;
            }
        }
        if ($eslId === null) {
            $sql = 'INSERT INTO esl_team (esl_name, esl_type) VALUES (:eslname, :esltype)';
            $bind = array(':eslname' => $_POST['frm_name'], ':esltype' => $_POST['frm_type']);
            $eslId = $this->executerSql($sql, $bind, 'esl_id');
            $action = ' créée';
        }
        else {
            $sql = 'INSERT INTO esl_team (esl_id, esl_name, esl_type) VALUES (:eslid, :eslname, :esltype)';
            $bind = array(':eslid' => $eslId, ':eslname' => $_POST['frm_name'], ':esltype' => $_POST['frm_type']);
            $this->executerSql($sql, $bind);
            $action = ' modifiée';
        }
        
        $sql = 'INSERT INTO esl_membres VALUES (:eslid, :accountid)';
        if (isset($_POST['frm_autres_membres']) === true && $_POST['frm_autres_membres'] !== '') {
            $addMembres = explode(',', $_POST['frm_autres_membres']);
            $_POST['frm_membres'] = array_merge($_POST['frm_membres'], $addMembres);
        }
        foreach ($_POST['frm_membres'] as $membre) {
            $this->executerSql($sql, array(':eslid' => $eslId, ':accountid' => $membre));
        }
        SmartbokMessage::addMessage(SmartbokMessage::INFO, 'Equipe '.$_POST['frm_name'].$action);
    }
    
    public function getTeams() {
        $sql = "SELECT * 
                FROM esl_team e 
                JOIN esl_membres m ON m.esl_id = e.esl_id
                WHERE m.Account_id = :accountid";
        $eslTeams = $this->executerSql($sql, array(':accountid' => $_SESSION['account_id']));
        return $eslTeams;
    }
    
    public function getTeamInfo($teamId) {
        $sql = "SELECT esl_name, ev.nom, ev.event_id
                FROM esl_team e
                JOIN events ev ON ev.event_id = e.esl_type
                WHERE esl_id = :eslid";
        $team = $this->executerSql($sql, array(':eslid' => $teamId));
        return $team[0];
    }
    
    public function getTeamMembers($teamId) {
        $sql = "SELECT account_id FROM esl_membres WHERE esl_id = :eslid";
        $team = $this->executerSql($sql, array(':eslid' => $teamId));
        return $team;
    }
    
    public function enregistrerStrat($type) {
        $sql = "INSERT INTO liens (lien_type, lien_name, lien_description, lien_url, lien_clan_id) VALUES (:type, :name, :desc, :lien, :clan)";
        $bind = array(':type' => $type,
                ':name' => $_POST['frm_nom'],
                ':desc' => $_POST['frm_description'],
                ':lien' => $_POST['frm_lien'],
                ':clan' => $_SESSION['accountInfo']['clanId']);
        $this->executerSql($sql, $bind);
    }
    
    public function getStratList($type) {
        $sql = "SELECT lien_id, lien_name, lien_description, lien_url FROM liens WHERE lien_clan_id = :clanid AND lien_type = :type";
        $lstMods = $this->executerSql($sql, array(':clanid' => $_SESSION['accountInfo']['clanId'], ':type' => $type));
        return $lstMods;
    }
}